
#pragma once

#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

class InetAddr
{
private:
    void GetAddress()
    {
        _port = ntohs(_addr.sin_port);
        _ip = inet_ntoa(_addr.sin_addr);
    }

public:
    InetAddr(const struct sockaddr_in &addr) : _addr(addr)
    {
        GetAddress();
    }
    InetAddr(const std::string&ip,uint16_t port):_ip(ip),_port(port)
    {
        _addr.sin_family=AF_INET;
        _addr.sin_port=htons(port);
        _addr.sin_addr.s_addr=inet_addr(ip.c_str());
    }
    InetAddr()
    {}
    std::string Ip()
    {
        return _ip;
    }
    bool operator == (const InetAddr &addr)
    {
        if(_ip == addr._ip && _port == addr._port)
        {
            return true;
        }
        return false;
    }
    struct sockaddr_in Addr()
    {
        return _addr;
    }
    uint16_t Port()
    {
        return _port;
    }
    ~InetAddr()
    {
    }

private:
    struct sockaddr_in _addr;//网络序列
    std::string _ip;//主机序列
    uint16_t _port;
};







